package com.ant.myagile

import grails.converters.JSON
import grails.plugins.springsecurity.Secured


class ProjectController {
	def projectService
	def springSecurityService
	def index() {
		redirect action: "list"
	}
	
	@Secured(['ROLE_USER','ROLE_ADMIN','ROLE_GUEST'])
	def list(){
		Member member = springSecurityService.currentUser
		[projects : projectService.findAllProjects(), myvalue: projectService.findTeams(), roles: member.getAuthorities().authority]
	}

	

	def save(){
		def project = projectService.save(params, request, servletContext)
		
		if(project){
			redirect(action: "list")
		}
	}

	def update(){
		def project = projectService.update(params, request, servletContext)
		if(project){
			redirect(action: "list")
		}
	}

	def findProject(){

		def project = projectService.findProject(params)
		def team = projectService.findAllTeams(params)
		render ([dtproject: project, dtteam: team?.team?.id] as JSON)
	}

	def checkExistProject(){
		def values = params.projectName
		def project = projectService.checkExistProject(values)
		if(project){
			render false
		}
		else {
			render true
		}
	}

	def removeProjectById() {
		def pid = request.getParameter("projectID");
		if(projectService.remove(pid)) {
			render(text : "success", contentType : "text/plain", encoding : "UTF-8")
			response.status = 200
		}
	}

	def removeTeamById(){
		render ([team: projectService.removeTeam(params.teamIDs, params.projectIDs)] as JSON)
	}


	def jsonFindProjectBelongToLoggedInMember() {
		def foundProjects = projectService.findProjectBelongToLoggedInMember()
		if(foundProjects) {
			render(contentType: 'text/json', {[projects : foundProjects, result : 'OK']})
			response.status = 200
		} else {
			render(contentType: 'text/json', {[result : 'No Project']})
			response.status = 200
		}
	}
}





